package com.ruoyi.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysCommoditySettingMapper;
import com.ruoyi.system.domain.SysCommoditySetting;
import com.ruoyi.system.service.ISysCommoditySettingService;

/**
 * 订单设置Service业务层处理
 *
 * @author ruoyi
 * @date 2021-11-25
 */
@Service
public class SysCommoditySettingServiceImpl implements ISysCommoditySettingService {

    private final Long STATUS = 1L;

    @Autowired
    private SysCommoditySettingMapper SettingMapper;

    /**
     * 查询订单设置
     *
     * @param id 订单设置主键
     * @return 订单设置
     */
    @Override
    public SysCommoditySetting selectSettingById(Long id) {
        return SettingMapper.selectSettingById(id);
    }

    /**
     * 查询订单设置
     *
     * @param ids 订单设置主键
     * @return 订单设置
     */
    @Override
    public List<Long> selectSettingStatusByIds(Long[] ids) {
        return SettingMapper.selectSettingStatusByIds(ids);
    }

    /**
     * 查询订单设置列表
     *
     * @param sysCommoditySetting 订单设置
     * @return 订单设置
     */
    @Override
    public List<SysCommoditySetting> selectSettingList(SysCommoditySetting sysCommoditySetting) {
        return SettingMapper.selectSettingList(sysCommoditySetting);
    }

    /**
     * 新增订单设置
     *
     * @param sysCommoditySetting 订单设置
     * @return 结果
     */
    @Override
    public int insertSetting(SysCommoditySetting sysCommoditySetting) {
        if (sysCommoditySetting.getStatus().equals(STATUS)) {
            List<SysCommoditySetting> commoditySettings = SettingMapper.selectSettingByStatus();
            if (commoditySettings != null && commoditySettings.size() > 0) {
                return -1;
            }
        }
        return SettingMapper.insertSetting(sysCommoditySetting);
    }

    /**
     * 修改订单设置
     *
     * @param sysCommoditySetting 订单设置
     * @return 结果
     */
    @Override
    public int updateSetting(SysCommoditySetting sysCommoditySetting) {
        if (sysCommoditySetting.getStatus().equals(STATUS)) {
            List<SysCommoditySetting> commoditySettings = SettingMapper.selectSettingByStatus();
            if (commoditySettings != null && commoditySettings.size() > 0) {
                return -1;
            }
        }
        return SettingMapper.updateSetting(sysCommoditySetting);
    }

    /**
     * 批量删除订单设置
     *
     * @param ids 需要删除的订单设置主键
     * @return 结果
     */
    @Override
    public int deleteSettingByIds(Long[] ids) {
        if (ids.length == SettingMapper.selectSettingCount()) {
            return -1;
        }

        List<Long> statusList = selectSettingStatusByIds(ids);
        for (Long status : statusList) if (STATUS.equals(status)) return -2;
        ;

        return SettingMapper.deleteSettingByIds(ids);
    }

    /**
     * 删除订单设置信息
     *
     * @param id 订单设置主键
     * @return 结果
     */
    @Override
    public int deleteSettingById(Long id) {
        SysCommoditySetting commoditySetting = selectSettingById(id);
        if (commoditySetting.getStatus().equals(STATUS)) {
            return -2;
        }
        return SettingMapper.deleteSettingById(id);
    }

    @Override
    public int changeSettingStatus(Long id, Long status) {
        return SettingMapper.changeSettingStatus(id, status);
    }
}
